/**
 * bootstrap table plugins
 */
(function() {
	'use strick',
	
	require.config({
		paths: {
			"bootstrap.tree": "bootstrap/extends/boostrap-treeview/1.2.0/js/bootstrap-treeview.min",
		},
		shim: {
			"bootstrap.table": {
				deps: [
				   "jquery", 
				   "css!bootstrap/extends/boostrap-treeview/1.2.0/css/bootstrap-treeview.min.css"
				],
				exports: "jQuery.fn.treeview"
			}
		}
	});
	
	define(['bootstrap.tree'], function(){
		return {
			"init": function(elem, options){
				var $elem = jQuery(elem);
				var _opts = jQuery.extend({}, {
					url: "/miwawa/{dbname}/common/tree.json".replace("{dbname}", dbname),
					data: $elem.data(),
					querys: {},
					levels: 5,
					multiSelect: true,
					showCheckbox: true,
					highlightSelected: false,
					onNodeChecked: null
				}, options);
				
				jQuery.ajaxsend({
					url: _opts.url,
					data : {
						"entityClass": _opts.data.entity,
						"offset": 0,
						"limit": 0,
						"order": _opts.querys.order || null,
						"and": _opts.querys.and || null
					},
					success: function(data){
						$elem.treeview({
							data: data,
							levels: _opts.levels,
							multiSelect: _opts.multiSelect,
							showCheckbox: _opts.showCheckbox,
							highlightSelected: _opts.highlightSelected,
							onNodeChecked: function(event, data){
								event.preventDefault;
								
								var nodeid = data.parentId;
								if(nodeid != undefined){
									$elem.treeview("checkNode", nodeid);
								}
							},
							onNodeUnchecked: function(event, data){
								event.preventDefault;
								
								var nodes = data.nodes;
								if(nodes != undefined){
									for(var i in nodes){
										$elem.treeview("uncheckNode", nodes[i].nodeId);
									}
								}
							}
						});
					}
				});
			}
		};
	});
})(this);
